Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert between integer and byte array #103

Closed
wants to merge 2 commits into from

Conversation

flier
Copy link
Contributor

@flier flier commented Feb 28, 2019

After Rust 1.32, the function to_<endian>_bytes and from_<endian>_bytes has stabilizations.

Cargo.toml Outdated Show resolved Hide resolved
build.rs Outdated Show resolved Hide resolved
src/int.rs Outdated Show resolved Hide resolved
@flier
Copy link
Contributor Author

flier commented Mar 5, 2019

Tests seem crashed at src/int.rs - int::PrimInt::unsigned_shr (line 173)

---- src/int.rs - int::PrimInt::unsigned_shr (line 173) stdout ----
error: literal out of range for i64
 --> src/int.rs:176:9
  |
6 | let n = 0xFEDCBA9876543210i64;
  |         ^^^^^^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0xFEDCBA9876543210u64`
  |
  = note: #[deny(overflowing_literals)] on by default
  = note: the literal `0xFEDCBA9876543210i64` (decimal `18364758544493064720`) does not fit into an `i64` and will become `-81985529216486896i64`
thread 'src/int.rs - int::PrimInt::unsigned_shr (line 173)' panicked at 'couldn't compile the test', src/librustdoc/test.rs:351:13
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

@cuviper cuviper mentioned this pull request Oct 3, 2020
@erikbrinkman
Copy link

The PR seems to have gone fallow, but if you come back to it, it'd be great to extend this to the float types as they also got these methods in 1.40.

@flier
Copy link
Contributor Author

flier commented Mar 4, 2021

Hi @erikbrinkman and @cuviper

The PR has be updated

  • The trait has been renamed to ToFromBytes
  • Use autocfg to check the expression
  • Implements ToFromBytes for integer and float

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Bytes implemented so generically, this could also support types like BigUint using Vec<u8>. Consuming self is not desirable in that case, so maybe the trait should use &self in the to_ methods? And likewise for from_, should they take &Self::Bytes? I expect the references would also get optimized away for the primitive cases anyway.

We do need to drop the breaking changes to existing traits. I would also like to see unit tests that cover each of the implemented primitive types.

src/float.rs Outdated

#[cfg(all(not(feature = "std"), feature = "libm"))]
use libm;

/// Generic trait for floating point numbers that works with `no_std`.
///
/// This trait implements a subset of the `Float` trait.
pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy + ToFromBytes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a breaking change to add requirements to the existing traits. ToFromBytes will have to stand alone.

src/int.rs Outdated
@@ -51,6 +51,7 @@ pub trait PrimInt:
+ CheckedMul<Output = Self>
+ CheckedDiv<Output = Self>
+ Saturating
+ ToFromBytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a breaking change.

@cuviper cuviper force-pushed the convert_between_int_and_bytes branch from efa9f80 to a922693 Compare February 10, 2023 22:25
@cuviper
Copy link
Member

cuviper commented Feb 10, 2023

Sorry, I pushed to this branch on accident, but the revived development is in #224.

@cuviper cuviper closed this Feb 10, 2023
bors bot added a commit that referenced this pull request Jul 20, 2023
224: Convert between int and bytes #189 r=cuviper a=kaidokert

Picking up #103 from `@flier` to rebase and factor in feedback. Making a quick draft first

Co-authored-by: Kaido Kert <kaidokert@gmail.com>
Co-authored-by: Josh Stone <cuviper@gmail.com>
Co-authored-by: kaidokert <kaidokert@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants